/*
 * Copyright 2010-2011 ESunny.com All right reserved. This software is the confidential and proprietary information of
 * ESunny.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with ESunny.com.
 */
package com.xjt.query.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;

import com.xjt.query.dao.interf.LoginLogDao;
import com.xjt.query.dao.pojo.LoginLog;

/**
 * 登录日志记录执行类
 * 
 * @author Jet Xu 2012-7-30 下午2:24:14
 */
public class LoginLogExecutor {

    private static final Logger    log  = Logger.getLogger(LoginLogExecutor.class);

    /** 日志任务线程池 */
    private static ExecutorService pool = Executors.newSingleThreadExecutor();

    /**
     * 记录登录日志
     * 
     * @param loginLogDao 日志执行DAO
     * @param logInfo 日志对象
     */
    public static void execute(LoginLogDao loginLogDao, LoginLog logInfo) {
        if (null == logInfo || null == loginLogDao) {
            return;
        }
        try {
            pool.execute(new AddLoginLogThread(loginLogDao, logInfo));
        } catch (Exception ex) {
            log.error("login log insert failed! userId = " + logInfo.getUserId(), ex);
        }
    }

    public static class AddLoginLogThread extends Thread {

        private LoginLogDao loginLogDao;
        private LoginLog    logInfo;

        public AddLoginLogThread(LoginLogDao loginLogDao, LoginLog logInfo){
            this.loginLogDao = loginLogDao;
            this.logInfo = logInfo;
        }

        @Override
        public void run() {
            if (null != loginLogDao && null != logInfo) {
                loginLogDao.insertLoginLog(logInfo);
            }
        }
    }
}
